package com.c2b.algorithm.leetcode.base;

/**
 * <a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list/">删除排序链表中的重复元素(Remove Duplicates from Sorted List)</a>
 * <p>给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。</p>
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *      输入：head = [1,1,2]
 *      输出：[1,2]
 *
 * 示例 2：
 *      输入：head = [1,1,2,3,3]
 *      输出：[1,2,3]
 * </pre>
 * </p>
 * <p>
 * <b>提示：</b>
 *     <ul>
 *          <li>链表中节点数目在范围 [0, 300] 内</li>
 *          <li>-100 <= Node.val <= 100</li>
 *          <li>题目数据保证链表已经按升序 排列</li>
 *     </ul>
 * </p>
 *
 * @author c2b
 * @see LC0082RemoveDuplicatesFromSortedList_II_M 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
 * @see LC0083RemoveDuplicatesFromSortedList_S 删除排序链表中的重复元素(Remove Duplicates from Sorted List)
 * @since 2023/12/5 16:03
 */
public class LC0083RemoveDuplicatesFromSortedList_S {

    static class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode currNode = head;
            while (currNode != null && currNode.next != null) {
                if (currNode.val == currNode.next.val) {
                    currNode.next = currNode.next.next;
                } else {
                    currNode = currNode.next;
                }
            }
            return head;
        }
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(1);
        head1.next.next = new ListNode(2);
        head1.next.next.next = new ListNode(3);

        ListNode head2 = new ListNode(1);
        head2.next = new ListNode(1);
        head2.next.next = new ListNode(2);
        head2.next.next.next = new ListNode(3);
        head2.next.next.next.next = new ListNode(3);

        Solution solution = new Solution();
        Printer.printListNode(solution.deleteDuplicates(head1));
        Printer.printListNode(solution.deleteDuplicates(head2));


    }
}
